<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-radio-group  v-model="timeType" button-style="solid" style="margin-bottom: 24px" v-if="needVariable">
      <a-radio-button value="default">{{$t('m.bpm.design.time_default')}}</a-radio-button>
      <a-radio-button value="variable">{{$t('m.bpm.design.time_variable')}}</a-radio-button>
    </a-radio-group>
    <a-form v-if="timeType === 'default'" layout="inline">
      <a-form-item :label="$t('m.bpm.design.time_default')" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%">
        <a-input-number id="days" v-model="days" :min="0" :step="1" style="width: 80px"/>
        {{$t('m.common.label.days')}}&nbsp;&nbsp;
        <a-input-number id="hours" v-model="hours" :max="23" :min="0" :step="1" style="width: 60px"/>
        {{$t('m.common.label.hours')}}&nbsp;&nbsp;
        <a-input-number id="minutes" v-model="minutes" :max="59" :min="0" :step="1" style="width: 60px"/>
        {{$t('m.common.label.minutes_short')}}&nbsp;&nbsp;
        <a-input-number id="seconds" v-model="seconds" :max="59" :min="0" :step="1" style="width: 60px"/>
        {{$t('m.common.label.seconds')}}
      </a-form-item>
    </a-form>
    <a-form v-else layout="inline">
      <a-form-item :label="$t('m.bpm.design.time_variable')" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%">
        <variableTag v-model="variableName" :placeholder="$t('m.bpm.design.time_variable')" @selected="selected"/>
      </a-form-item>
    </a-form>
    <a-form layout="inline" v-if="needMultiTimes">
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%">
        <a-checkbox :checked="multiTimes" @change="changeMultiTimes">
          {{$t('m.bpm.design.multi_times')}}
        </a-checkbox>
      </a-form-item>
      <a-form-item v-if="multiTimes" :label="$t('m.bpm.design.time_interval')" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%">
        <a-input-number id="days_interval" v-model="days_interval" :min="0" :step="1" style="width: 80px"/>
        {{$t('m.common.label.days')}}&nbsp;&nbsp;
        <a-input-number id="hours_interval" v-model="hours_interval" :max="23" :min="0" :step="1" style="width: 60px"/>
        {{$t('m.common.label.hours')}}&nbsp;&nbsp;
        <a-input-number id="minutes_interval" v-model="minutes_interval" :max="59" :min="0" :step="1" style="width: 60px"/>
        {{$t('m.common.label.minutes_short')}}&nbsp;&nbsp;
        <a-input-number id="seconds_interval" v-model="seconds_interval" :max="59" :min="0" :step="1" style="width: 60px"/>
        {{$t('m.common.label.seconds')}}
      </a-form-item>
      <a-form-item v-if="multiTimes" :label="$t('m.bpm.design.repeat_times')" :labelCol="labelCol" :wrapperCol="wrapperCol" style="width: 100%">
        <a-input-number id="repeat_times" v-model="repeat_times" :min="1" :step="1" style="width: 80px"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>

  import variableTag from "./variableTag";

  export default {
    name: "TaskTime",
    components: {
      variableTag,
    },
    data() {
      return {
        title: this.$t('m.bpm.design.time_set'),
        needVariable: false,
        needMultiTimes: false,
        multiTimes: false,
        width: 700,
        visible: false,
        days: null,
        hours: null,
        minutes: null,
        seconds: null,
        days_interval: null,
        hours_interval: null,
        minutes_interval: null,
        seconds_interval: null,
        repeat_times: 1,
        timeType: 'default',
        labelCol: {
          span: 3
        },
        wrapperCol: {
          span: 20
        },
        variableName: null,
        variableTitle: null,
      }
    },
    methods: {
      handleOk() {
        let alias = "";
        let newValue = "";
        if (this.timeType === 'default') {
          newValue = "P";
          if (this.days != null && this.days > 0) {
            alias += this.days + this.$t('m.common.label.days');
            newValue += this.days + "DT";
          } else {
            newValue += "0DT";
          }
          if (this.hours != null && this.hours > 0) {
            alias += this.hours + this.$t('m.common.label.hours');
            newValue += this.hours + "H";
          } else {
            newValue += "0H";
          }
          if (this.minutes != null && this.minutes > 0) {
            alias += this.minutes + this.$t('m.common.label.minutes_short');
            newValue += this.minutes + "M";
          } else {
            newValue += "0M";
          }
          if (this.seconds != null && this.seconds > 0) {
            alias += this.seconds + this.$t('m.common.label.seconds');
            newValue += this.seconds + "S";
          } else {
            newValue += "0S";
          }
          if (newValue === "P0DT0H0M0S") {
            newValue = null;
            alias = null;
          }
        } else {
          if (this.variableName && this.variableTitle) {
            newValue = "${" + this.variableName + "}";
            alias = this.variableTitle + "(" + newValue + ")";
          }
        }
        if (newValue && alias && this.needMultiTimes && this.multiTimes) {
          const intervalTime = this.getIntervalTime();
          if (intervalTime !== "P0DT0H0M0S") {
            newValue = newValue + "#true" + "#" + intervalTime + "#" + this.repeat_times
          }
        }
        this.$emit('selectFinished', alias, newValue);
        this.handleCancel();
      },
      getIntervalTime() {
        let newValue = "P";
        if (this.days_interval != null && this.days_interval > 0) {
          newValue += this.days_interval + "DT";
        } else {
          newValue += "0DT";
        }
        if (this.hours_interval != null && this.hours_interval > 0) {
          newValue += this.hours_interval + "H";
        } else {
          newValue += "0H";
        }
        if (this.minutes_interval != null && this.minutes_interval > 0) {
          newValue += this.minutes_interval + "M";
        } else {
          newValue += "0M";
        }
        if (this.seconds_interval != null && this.seconds_interval > 0) {
          newValue += this.seconds_interval + "S";
        } else {
          newValue += "0S";
        }
        return newValue
      },
      handleCancel() {
        this.visible = false;
        this.days = null;
        this.hours = null;
        this.minutes = null;
        this.seconds = null;
        this.days_interval = null;
        this.hours_interval = null;
        this.minutes_interval = null;
        this.seconds_interval = null;
        this.repeat_times = 1;
        this.timeType = 'default';
        this.multiTimes = false;
        this.variableName = null;
        this.variableTitle = null;
      },
      selected(alias) {
        this.variableTitle = alias;
      },
      changeMultiTimes(e) {
        this.multiTimes = e.target.checked
      },
      initData(oldData) {
        if (!oldData) {
          return
        }
        if (this.needMultiTimes) {
          let dateValue = oldData;
          let intervalValue = null;
          let times = null;
          if (oldData.indexOf("#") !== -1) {
            const valueArr = oldData.split("#")
            dateValue = valueArr[0]
            this.multiTimes = valueArr[1] === 'true';
            intervalValue = valueArr[2]
            if (valueArr.length >= 4) {
              times = valueArr[3]
            }
          }
          // P0DT0H0M0S
          this.days = dateValue.substring(1, dateValue.indexOf("DT"));
          this.hours = dateValue.substring(dateValue.indexOf("DT") + 2, oldData.indexOf("H"));
          this.minutes = dateValue.substring(dateValue.indexOf("H") + 1, oldData.indexOf("M"));
          this.seconds = dateValue.substring(dateValue.indexOf("M") + 1, oldData.indexOf("S"));
          if (intervalValue) {
            this.days_interval = intervalValue.substring(1, intervalValue.indexOf("DT"));
            this.hours_interval = intervalValue.substring(intervalValue.indexOf("DT") + 2, intervalValue.indexOf("H"));
            this.minutes_interval = intervalValue.substring(intervalValue.indexOf("H") + 1, intervalValue.indexOf("M"));
            this.seconds_interval = intervalValue.substring(intervalValue.indexOf("M") + 1, intervalValue.indexOf("S"));
            if (times !== null) {
              this.repeat_times = Number(times)
            }
          }
        } else {
          // 暂时不实现合理完成时间的设置，因为有流程变量的情况
        }
      }
    }
  }
</script>
